package com.praxinfosystems.sarthak.service;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

//import org.apache.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.praxinfosystems.sarthak.common.AuthenticatedUserDetails;
import com.praxinfosystems.sarthak.domain.User;

@Service("myUserDetailService")
public class CrmUserDetailService implements UserDetailsService{
	
	
//protected static Logger logger = Logger.getLogger("service");
	
	@Resource(name="userService")
	private UserService userService;

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
//		System.out.println("reached inuser detail");
		User user=userService.getUserByUserId(username);
		if(null!=user){
			Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();
			SimpleGrantedAuthority authority= new SimpleGrantedAuthority(user.getRole());
			roles.add(authority);
			AuthenticatedUserDetails authenticatedUserDetails = new AuthenticatedUserDetails(user.getUserId(),user.getPassword(),roles,true,true,true,true); 
			return authenticatedUserDetails;
		}else{
			throw new UsernameNotFoundException("user does not exist");
		}
		
	}

}
